import sys
import requests

__version__ = "0.6"

MY_PYTHON_VERSION = str(sys.version_info.major) + "." +\
                    str(sys.version_info.minor) + "." +\
                    str(sys.version_info.micro) # + "_" +\
                    # str(sys.version_info.releaselevel) + "_" +\
                    # str(sys.version_info.serial)

USER_AGENT = "Python/" + MY_PYTHON_VERSION + " " +\
            "python_requests/" + str(requests.__version__) + " " +\
            f"cloudflare-better-node/{__version__} (github.com@xfl12345) "

# download 线程状态常量
STATUS_INIT = 0
STATUS_READY = 1
STATUS_RUNNING = 2
STATUS_WORK_FINISHED = 3
STATUS_EXIT = 4
STATUS_FORCE_EXIT = 5
STATUS_PAUSE = 6

# 对 download 过程中的 getsize 约束程度
LEVEL_ENFORCE = 10     # 绝对精准，精准至byte级别
LEVEL_PERMISSIVE = 11  # 宽松，达量即可，允许超量

# downloader 任务状态
DL_FIRST_BORN = 20
DL_INITIATING = 21
DL_RUNNING = 22
DL_COMPLETE = 23
DL_FAILED = 29

# scanner deep level
SCAN_NORMAL = 30
SCAN_MORE_DEEPER = 31
SCAN_DEEPEST = 32

ONE_BIN_KB:int = 1024               # 2^10
ONE_BIN_MB:int = 0x100000           # 2^20
ONE_BIN_GB:int = 0x40000000         # 2^30
ONE_BIN_TB:int = 0x10000000000      # 2^40
ONE_BIN_PB:int = 0x4000000000000    # 2^50

ONE_OF_1024:float = 0.0009765625 # 1/1024

SPEEDTEST_DEFAULT_RANGE = (0, 60 * ONE_BIN_MB -1)

# source code URL: https://github.com/Narengowda/http_headers/blob/master/headers.py
class HeaderFieldNames(object):
    _IM = "A-IM"
    ALPN = "ALPN"
    ACCEPT = "Accept"
    ACCEPT_ADDITIONS = "Accept-Additions"
    ACCEPT_CHARSET = "Accept-Charset"
    ACCEPT_DATETIME = "Accept-Datetime"
    ACCEPT_ENCODING = "Accept-Encoding"
    ACCEPT_FEATURES = "Accept-Features"
    ACCEPT_LANGUAGE = "Accept-Language"
    ACCEPT_PATCH = "Accept-Patch"
    ACCEPT_POST = "Accept-Post"
    ACCEPT_RANGES = "Accept-Ranges"
    AGE = "Age"
    ALLOW = "Allow"
    ALSO_CONTROL = "Also-Control"
    ALT_SVC = "Alt-Svc"
    ALT_USED = "Alt-Used"
    ALTERNATE_RECIPIENT = "Alternate-Recipient"
    ALTERNATES = "Alternates"
    APPLY_TO_REDIRECT_REF = "Apply-To-Redirect-Ref"
    APPROVED = "Approved"
    ARCHIVE = "Archive"
    ARCHIVED_AT = "Archived-At"
    ARTICLE_NAMES = "Article-Names"
    ARTICLE_UPDATES = "Article-Updates"
    AUTHENTICATION_CONTROL = "Authentication-Control"
    AUTHENTICATION_INFO = "Authentication-Info"
    AUTHENTICATION_RESULTS = "Authentication-Results"
    AUTHORIZATION = "Authorization"
    AUTO_SUBMITTED = "Auto-Submitted"
    AUTOFORWARDED = "Autoforwarded"
    AUTOSUBMITTED = "Autosubmitted"
    BASE = "Base"
    BCC = "Bcc"
    BODY = "Body"
    C_EXT = "C-Ext"
    C_MAN = "C-Man"
    C_OPT = "C-Opt"
    C_PEP = "C-PEP"
    C_PEP_INFO = "C-PEP-Info"
    CACHE_CONTROL = "Cache-Control"
    CALDAV_TIMEZONES = "CalDAV-Timezones"
    CC = "Cc"
    CLOSE = "Close"
    COMMENTS = "Comments"
    CONNECTION = "Connection"
    CONTENT_ALTERNATIVE = "Content-Alternative"
    CONTENT_BASE = "Content-Base"
    CONTENT_DESCRIPTION = "Content-Description"
    CONTENT_DISPOSITION = "Content-Disposition"
    CONTENT_DURATION = "Content-Duration"
    CONTENT_ENCODING = "Content-Encoding"
    CONTENT_ID = "Content-ID"
    CONTENT_IDENTIFIER = "Content-Identifier"
    CONTENT_LANGUAGE = "Content-Language"
    CONTENT_LENGTH = "Content-Length"
    CONTENT_LOCATION = "Content-Location"
    CONTENT_MD5 = "Content-MD5"
    CONTENT_RANGE = "Content-Range"
    CONTENT_RETURN = "Content-Return"
    CONTENT_SCRIPT_TYPE = "Content-Script-Type"
    CONTENT_STYLE_TYPE = "Content-Style-Type"
    CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"
    CONTENT_TYPE = "Content-Type"
    CONTENT_VERSION = "Content-Version"
    CONTENT_FEATURES = "Content-features"
    CONTROL = "Control"
    CONVERSION = "Conversion"
    CONVERSION_WITH_LOSS = "Conversion-With-Loss"
    COOKIE = "Cookie"
    COOKIE2 = "Cookie2"
    DASL = "DASL"
    DAV = "DAV"
    DKIM_SIGNATURE = "DKIM-Signature"
    DL_EXPANSION_HISTORY = "DL-Expansion-History"
    DATE = "Date"
    DATE_RECEIVED = "Date-Received"
    DEFAULT_STYLE = "Default-Style"
    DEFERRED_DELIVERY = "Deferred-Delivery"
    DELIVERY_DATE = "Delivery-Date"
    DELTA_BASE = "Delta-Base"
    DEPTH = "Depth"
    DERIVED_FROM = "Derived-From"
    DESTINATION = "Destination"
    DIFFERENTIAL_ID = "Differential-ID"
    DIGEST = "Digest"
    DISCARDED_X400_IPMS_EXTENSIONS = "Discarded-X400-IPMS-Extensions"
    DISCARDED_X400_MTS_EXTENSIONS = "Discarded-X400-MTS-Extensions"
    DISCLOSE_RECIPIENTS = "Disclose-Recipients"
    DISPOSITION_NOTIFICATION_OPTIONS = "Disposition-Notification-Options"
    DISPOSITION_NOTIFICATION_TO = "Disposition-Notification-To"
    DISTRIBUTION = "Distribution"
    DOWNGRADED_BCC = "Downgraded-Bcc"
    DOWNGRADED_CC = "Downgraded-Cc"
    DOWNGRADED_DISPOSITION_NOTIFICATION_TO = "Downgraded-Disposition-Notification-To"
    DOWNGRADED_FINAL_RECIPIENT = "Downgraded-Final-Recipient"
    DOWNGRADED_FROM = "Downgraded-From"
    DOWNGRADED_IN_REPLY_TO = "Downgraded-In-Reply-To"
    DOWNGRADED_MAIL_FROM = "Downgraded-Mail-From"
    DOWNGRADED_MESSAGE_ID = "Downgraded-Message-Id"
    DOWNGRADED_ORIGINAL_RECIPIENT = "Downgraded-Original-Recipient"
    DOWNGRADED_RCPT_TO = "Downgraded-Rcpt-To"
    DOWNGRADED_REFERENCES = "Downgraded-References"
    DOWNGRADED_REPLY_TO = "Downgraded-Reply-To"
    DOWNGRADED_RESENT_BCC = "Downgraded-Resent-Bcc"
    DOWNGRADED_RESENT_CC = "Downgraded-Resent-Cc"
    DOWNGRADED_RESENT_FROM = "Downgraded-Resent-From"
    DOWNGRADED_RESENT_REPLY_TO = "Downgraded-Resent-Reply-To"
    DOWNGRADED_RESENT_SENDER = "Downgraded-Resent-Sender"
    DOWNGRADED_RESENT_TO = "Downgraded-Resent-To"
    DOWNGRADED_RETURN_PATH = "Downgraded-Return-Path"
    DOWNGRADED_SENDER = "Downgraded-Sender"
    DOWNGRADED_TO = "Downgraded-To"
    ETAG = "ETag"
    ENCODING = "Encoding"
    ENCRYPTED = "Encrypted"
    EXPECT = "Expect"
    EXPIRES = "Expires"
    EXPIRY_DATE = "Expiry-Date"
    EXT = "Ext"
    FOLLOWUP_TO = "Followup-To"
    FORWARDED = "Forwarded"
    FROM = "From"
    GENERATE_DELIVERY_REPORT = "Generate-Delivery-Report"
    GETPROFILE = "GetProfile"
    HTTP2_SETTINGS = "HTTP2-Settings"
    HOBAREG = "Hobareg"
    HOST = "Host"
    IM = "IM"
    IF = "If"
    IF_MATCH = "If-Match"
    IF_MODIFIED_SINCE = "If-Modified-Since"
    IF_NONE_MATCH = "If-None-Match"
    IF_RANGE = "If-Range"
    IF_SCHEDULE_TAG_MATCH = "If-Schedule-Tag-Match"
    IF_UNMODIFIED_SINCE = "If-Unmodified-Since"
    IMPORTANCE = "Importance"
    IN_REPLY_TO = "In-Reply-To"
    INCOMPLETE_COPY = "Incomplete-Copy"
    INJECTION_DATE = "Injection-Date"
    INJECTION_INFO = "Injection-Info"
    KEEP_ALIVE = "Keep-Alive"
    KEYWORDS = "Keywords"
    LABEL = "Label"
    LANGUAGE = "Language"
    LAST_MODIFIED = "Last-Modified"
    LATEST_DELIVERY_TIME = "Latest-Delivery-Time"
    LINES = "Lines"
    LINK = "Link"
    LIST_ARCHIVE = "List-Archive"
    LIST_HELP = "List-Help"
    LIST_ID = "List-ID"
    LIST_OWNER = "List-Owner"
    LIST_POST = "List-Post"
    LIST_SUBSCRIBE = "List-Subscribe"
    LIST_UNSUBSCRIBE = "List-Unsubscribe"
    LIST_UNSUBSCRIBE_POST = "List-Unsubscribe-Post"
    LOCATION = "Location"
    LOCK_TOKEN = "Lock-Token"
    MIME_VERSION = "MIME-Version"
    MMHS_ACP127_MESSAGE_IDENTIFIER = "MMHS-Acp127-Message-Identifier"
    MMHS_CODRESS_MESSAGE_INDICATOR = "MMHS-Codress-Message-Indicator"
    MMHS_COPY_PRECEDENCE = "MMHS-Copy-Precedence"
    MMHS_EXEMPTED_ADDRESS = "MMHS-Exempted-Address"
    MMHS_EXTENDED_AUTHORISATION_INFO = "MMHS-Extended-Authorisation-Info"
    MMHS_HANDLING_INSTRUCTIONS = "MMHS-Handling-Instructions"
    MMHS_MESSAGE_INSTRUCTIONS = "MMHS-Message-Instructions"
    MMHS_MESSAGE_TYPE = "MMHS-Message-Type"
    MMHS_ORIGINATOR_PLAD = "MMHS-Originator-PLAD"
    MMHS_ORIGINATOR_REFERENCE = "MMHS-Originator-Reference"
    MMHS_OTHER_RECIPIENTS_INDICATOR_CC = "MMHS-Other-Recipients-Indicator-CC"
    MMHS_OTHER_RECIPIENTS_INDICATOR_TO = "MMHS-Other-Recipients-Indicator-To"
    MMHS_PRIMARY_PRECEDENCE = "MMHS-Primary-Precedence"
    MMHS_SUBJECT_INDICATOR_CODES = "MMHS-Subject-Indicator-Codes"
    MT_PRIORITY = "MT-Priority"
    MAN = "Man"
    MAX_FORWARDS = "Max-Forwards"
    MEMENTO_DATETIME = "Memento-Datetime"
    MESSAGE_CONTEXT = "Message-Context"
    MESSAGE_ID = "Message-ID"
    MESSAGE_TYPE = "Message-Type"
    METER = "Meter"
    NNTP_POSTING_DATE = "NNTP-Posting-Date"
    NNTP_POSTING_HOST = "NNTP-Posting-Host"
    NEGOTIATE = "Negotiate"
    NEWSGROUPS = "Newsgroups"
    OBSOLETES = "Obsoletes"
    OPT = "Opt"
    OPTIONAL_WWW_AUTHENTICATE = "Optional-WWW-Authenticate"
    ORDERING_TYPE = "Ordering-Type"
    ORGANIZATION = "Organization"
    ORIGIN = "Origin"
    ORIGINAL_ENCODED_INFORMATION_TYPES = "Original-Encoded-Information-Types"
    ORIGINAL_FROM = "Original-From"
    ORIGINAL_MESSAGE_ID = "Original-Message-ID"
    ORIGINAL_RECIPIENT = "Original-Recipient"
    ORIGINAL_SENDER = "Original-Sender"
    ORIGINAL_SUBJECT = "Original-Subject"
    ORIGINATOR_RETURN_ADDRESS = "Originator-Return-Address"
    OVERWRITE = "Overwrite"
    P3P = "P3P"
    PEP = "PEP"
    PICS_LABEL = "PICS-Label"
    PATH = "Path"
    PEP_INFO = "Pep-Info"
    POSITION = "Position"
    POSTING_VERSION = "Posting-Version"
    PRAGMA = "Pragma"
    PREFER = "Prefer"
    PREFERENCE_APPLIED = "Preference-Applied"
    PREVENT_NONDELIVERY_REPORT = "Prevent-NonDelivery-Report"
    PRIORITY = "Priority"
    PROFILEOBJECT = "ProfileObject"
    PROTOCOL = "Protocol"
    PROTOCOL_INFO = "Protocol-Info"
    PROTOCOL_QUERY = "Protocol-Query"
    PROTOCOL_REQUEST = "Protocol-Request"
    PROXY_AUTHENTICATE = "Proxy-Authenticate"
    PROXY_AUTHENTICATION_INFO = "Proxy-Authentication-Info"
    PROXY_AUTHORIZATION = "Proxy-Authorization"
    PROXY_FEATURES = "Proxy-Features"
    PROXY_INSTRUCTION = "Proxy-Instruction"
    PUBLIC = "Public"
    PUBLIC_KEY_PINS = "Public-Key-Pins"
    PUBLIC_KEY_PINS_REPORT_ONLY = "Public-Key-Pins-Report-Only"
    RANGE = "Range"
    RECEIVED = "Received"
    RECEIVED_SPF = "Received-SPF"
    REDIRECT_REF = "Redirect-Ref"
    REFERENCES = "References"
    REFERER = "Referer"
    RELAY_VERSION = "Relay-Version"
    REPLY_BY = "Reply-By"
    REPLY_TO = "Reply-To"
    REQUIRE_RECIPIENT_VALID_SINCE = "Require-Recipient-Valid-Since"
    RESENT_BCC = "Resent-Bcc"
    RESENT_CC = "Resent-Cc"
    RESENT_DATE = "Resent-Date"
    RESENT_FROM = "Resent-From"
    RESENT_MESSAGE_ID = "Resent-Message-ID"
    RESENT_REPLY_TO = "Resent-Reply-To"
    RESENT_SENDER = "Resent-Sender"
    RESENT_TO = "Resent-To"
    RETRY_AFTER = "Retry-After"
    RETURN_PATH = "Return-Path"
    SLUG = "SLUG"
    SAFE = "Safe"
    SCHEDULE_REPLY = "Schedule-Reply"
    SCHEDULE_TAG = "Schedule-Tag"
    SEC_WEBSOCKET_ACCEPT = "Sec-WebSocket-Accept"
    SEC_WEBSOCKET_EXTENSIONS = "Sec-WebSocket-Extensions"
    SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key"
    SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol"
    SEC_WEBSOCKET_VERSION = "Sec-WebSocket-Version"
    SECURITY_SCHEME = "Security-Scheme"
    SEE_ALSO = "See-Also"
    SENDER = "Sender"
    SENSITIVITY = "Sensitivity"
    SERVER = "Server"
    SET_COOKIE = "Set-Cookie"
    SET_COOKIE2 = "Set-Cookie2"
    SETPROFILE = "SetProfile"
    SOAPACTION = "SoapAction"
    SOLICITATION = "Solicitation"
    STATUS_URI = "Status-URI"
    STRICT_TRANSPORT_SECURITY = "Strict-Transport-Security"
    SUBJECT = "Subject"
    SUMMARY = "Summary"
    SUPERSEDES = "Supersedes"
    SURROGATE_CAPABILITY = "Surrogate-Capability"
    SURROGATE_CONTROL = "Surrogate-Control"
    TCN = "TCN"
    TE = "TE"
    TTL = "TTL"
    TIMEOUT = "Timeout"
    TO = "To"
    TOPIC = "Topic"
    TRAILER = "Trailer"
    TRANSFER_ENCODING = "Transfer-Encoding"
    URI = "URI"
    UPGRADE = "Upgrade"
    URGENCY = "Urgency"
    USER_AGENT = "User-Agent"
    VBR_INFO = "VBR-Info"
    VARIANT_VARY = "Variant-Vary"
    VARY = "Vary"
    VIA = "Via"
    WWW_AUTHENTICATE = "WWW-Authenticate"
    WANT_DIGEST = "Want-Digest"
    WARNING = "Warning"
    X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options"
    X_FRAME_OPTIONS = "X-Frame-Options"
    X400_CONTENT_IDENTIFIER = "X400-Content-Identifier"
    X400_CONTENT_RETURN = "X400-Content-Return"
    X400_CONTENT_TYPE = "X400-Content-Type"
    X400_MTS_IDENTIFIER = "X400-MTS-Identifier"
    X400_ORIGINATOR = "X400-Originator"
    X400_RECEIVED = "X400-Received"
    X400_RECIPIENTS = "X400-Recipients"
    X400_TRACE = "X400-Trace"
    XREF = "Xref"
    # Cloudflare API Headers
    X_AUTH_EMAIL = "X-Auth-Email"
    X_AUTH_KEY = "X-Auth-Key"


HEADER_FIELD_NAMES = HeaderFieldNames()